/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.learn.learn_view.dialog;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

/**
 * An abstract class extending JComponent representing a dialog on screen.
 * This class provides a tabbed pane for the dialog.
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.Interface
 * @since January 2004
 */
public  class TabbedDialog extends JComponent implements ActionListener {
	
    private static final long serialVersionUID = 1L;
	public static final int OK_OPTION = 0;
    public static final int CANCEL_OPTION = 1;
	
    private int command;
    //private JScrollPane sPane = new JScrollPane();
    private JTabbedPane tabbedPane = new JTabbedPane();
    private JDialog dlg;
    private JPanel confirmationPane = new JPanel();
    private JButton ok = new JButton("OK");
    private JButton cancel = new JButton("Cancel");
	
//*****************************************************************************
	
    public int showDialog(JFrame parent, String title) {
		dlg = new JDialog(parent, title, true);
        dlg.setSize(new Dimension(300, 200));
        
        dlg.setLocationRelativeTo(parent);
        dlg.setResizable(false);
        dlg.getContentPane().setLayout(new BorderLayout());
  
        JScrollPane spane = new JScrollPane(tabbedPane);
        
        dlg.getContentPane().add(spane, BorderLayout.CENTER);
        
        dlg.getContentPane().add(confirmationPane, BorderLayout.SOUTH);
        dlg.getRootPane().setDefaultButton(ok);
        
		
        //Sets up confirmation pane
        confirmationPane.add(ok);
        ok.addActionListener(this);
		
        cancel.setMnemonic(KeyEvent.VK_C);
        confirmationPane.add(cancel);
        cancel.addActionListener(this);
        dlg.pack();
        
        Dimension d = dlg.getSize();
        Dimension ideal = new Dimension(1000,600);
        
        if(d.height<ideal.height || d.width<ideal.width){
        	spane.setPreferredSize(d);
        }
        else{
        	spane.setPreferredSize(ideal);
        }  
        spane.createHorizontalScrollBar();
     	spane.createVerticalScrollBar();
     	spane.revalidate();
     	spane.repaint();
     	
        dlg.setVisible(true);
     	
        return command;
    }
	
//*****************************************************************************
	
    public void actionPerformed(ActionEvent e) {
        JButton btn = (JButton)e.getSource();
        if (btn.getActionCommand() == "OK"){
            command = OK_OPTION;
            approvedPerform();
        }else{
            command = CANCEL_OPTION;
        }
        dlg.setVisible(false);
    }
	
//*****************************************************************************
	
    protected void approvedPerform(){};
	
//*****************************************************************************
	
    public void addTab(String title, Component component) {
        tabbedPane.addTab(title, component);
    }
    
    
//*****************************************************************************
	
    protected void cancel() {
        command = CANCEL_OPTION;
    }
    
//*****************************************************************************
	
	public void setButtonsEnabled(boolean val) {
		ok.setEnabled(val);
		cancel.setEnabled(val);
	}
	
//*****************************************************************************

	public JDialog getContent(){
		return dlg;
	}
	
	public JTabbedPane getTablePane(){
	    return this.tabbedPane;
	}
}
